import { Data,DataMap, Config, Delta } from '../data.js'

function normalize(val, base) {
    return val / base
}
function denormalize(val, base) {
    return val * base
}
export function run() {
    const delta = {}
    const transform = Object.assign({},Delta)


    for(let transI of Object.keys(transform)){

        transform[transI] = normalize( transform[transI],DataMap[transI].base)
    }



    const effectsFrom = {}
    for (let dataIte of Data) {
        for (let effectIte of dataIte.effects) {
            effectsFrom[effectIte[0]] ??= []
            effectsFrom[effectIte[0]].push([dataIte.id, effectIte[1]])
        }
    }

    for (let i = 1; i <= Config.step; i++) {

        for (let dataIte of Data) {
            const id = dataIte.id
            let baseTransform = 0
            if (i === 1) {
                baseTransform = transform[dataIte.id] ?? 0
            }




            for (let effectIte of effectsFrom[id] ?? []) {
                baseTransform += (transform[effectIte[0]] ?? 0) * effectIte[1] * (Config.stepRate ** i)
            }
            transform[id] = baseTransform

            delta[id] = (delta[id] ?? 0) + denormalize(baseTransform,DataMap[id].base)


        }

      
    }
    const r = Data.reduce((pv,cv)=>{

        pv[cv.id]=delta[cv.id]+cv.default
        return pv
    },{})

    return {
        raw:Data,
        value:r
    }

}
